/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import core.Course;
import core.Program;
import core.School;
import core.User;
import db.JDBC.JdbcDaoFactory;
import db.JDBC.core.SchoolJdbcDAOCore;
import db.JDBC.core.UserJdbcDAOCore;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import web.SuperSchoolUserObject;


/**
 *
 * @author Anton
 */

public class SchoolTreeBean extends BaseBean
{

    
    @PostConstruct
    public void init()
    {
        initTree();
    }

    public void addProgram(ActionEvent event)
    {
            Long programId = (Long)event.getComponent().getAttributes().get("programId");
            if(programId != null)
            {
                Program program = JdbcDaoFactory.getInstance().getProgramJdbcDao().getProgramById(programId);
                if(program != null)
                {
                    User currentUser = sessionBean.getCurrentUser();
                    if(currentUser != null)
                        UserJdbcDAOCore.getInstance().addProgramToUser(currentUser, program);
                }
            }
    }

    public void addCourse(ActionEvent event)
    {
        Long courseId = (Long)event.getComponent().getAttributes().get("courseId");
        if(courseId != null)
        {
            Course course = JdbcDaoFactory.getInstance().getCourseJdbcDao().getCourseById(courseId);
            if(course != null)
            {
                User currentUser = sessionBean.getCurrentUser();
                if(currentUser != null)
                    UserJdbcDAOCore.getInstance().addCourseToUser(currentUser, course);
            }
        }
    }

    private void initTree()
    {
        counter = 0;
        DefaultMutableTreeNode rootNode = addNode(null, "Schools" ,SuperSchoolUserObject.rootTreeType, counter++, null);

        model = new DefaultTreeModel(rootNode);
        selectedUserObject = (SuperSchoolUserObject) rootNode.getUserObject();
        selectedUserObject.setExpanded(true);
        
        Collection<School> schools = SchoolJdbcDAOCore.getInstance().getAllSchools();

        if(schools != null)
        {
            for(School s : schools)
            {
                DefaultMutableTreeNode schoolNode = addNode(rootNode, s.getName(),SuperSchoolUserObject.schoolListTreeType, counter++, s);

                if(s.getPrograms() != null)
                {
                    DefaultMutableTreeNode schoolNodePrograms = addNode(schoolNode, "Programs", SuperSchoolUserObject.programListTreeType, counter++, s);
                    for (Program p : s.getPrograms())
                    {
                        DefaultMutableTreeNode programNode = addNode(schoolNodePrograms, p.getName(), SuperSchoolUserObject.programTreeType, counter++, p);
                        if(p.getCourses() != null)
                        {
                            for(Course c : p.getCourses())
                            {
                                addNode(programNode, null, SuperSchoolUserObject.courseTreeType, counter++, c);
                            }
                        }
                    }
                }
                if(s.getCourses() != null)
                {
                    DefaultMutableTreeNode schoolNodeCourses = addNode(schoolNode, "Courses", SuperSchoolUserObject.courseListTreeType, counter++, s);
                    for(Course c : s.getCourses())
                    {
                        addNode(schoolNodeCourses, null, SuperSchoolUserObject.courseTreeType, counter++, c);
                    }
                }

            }
        }
    }    
}
